package com.hello.booksale.service;

import com.hello.booksale.dao.UserDao;
import com.hello.booksale.entity.AdminEntity;
import com.hello.booksale.entity.UserEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserService implements UserDetailsService {

    @Resource
    private UserDao userDao;

    @Resource
    private AdminEntity admin;

    public List<UserEntity> selectAllUsers(){
        return userDao.selectAllUsers();
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if(Objects.equals(username, "admin"))
        {
            return admin;
        }else{
            UserEntity userEntity = userDao.selectUserName(username);
            if (userEntity == null) {
                throw new UsernameNotFoundException("用户不存在!");
            }
            return userEntity;
        }
    }


}
